package com.urban.pooling.util;

import java.util.ArrayList;
import java.util.List;

import com.urban.pooling.domain.GeoPoint;
import com.urban.pooling.domain.Route;
import com.urban.pooling.domain.RouteRequest;
import com.urban.pooling.domain.RouteRequestStatus;
import com.urban.pooling.domain.User;
import com.urban.pooling.dto.GeoPointDto;
import com.urban.pooling.dto.RouteDto;
import com.urban.pooling.dto.RouteDto.RouteDtoBuilder;
import com.urban.pooling.dto.RouteRequestDto;
import com.urban.pooling.dto.RouteRequestDto.RouteRequestDtoBuilder;

public class DomainUtil {

	public static RouteDto createDto(Route route) {
		List<GeoPointDto> waypoints = new ArrayList<GeoPointDto>();
		if (route.getWaypoints() != null) {
			for (GeoPoint waypoint : route.getWaypoints()) {
				waypoints.add(createDto(waypoint));
			}
		}
		return new RouteDtoBuilder().routeId(route.getRouteId()).car(route.getCar())
				.seats(route.getSeats() - route.getOccupiedSeats()).startAddress(route.getStartAddress())
				.destinationAddress(route.getDestAddress()).startDateTime(route.getStartTime())
				.arrivalDateTime(route.getArrivalTime()).startLocation(createDto(route.getStartPoint()))
				.destinationLocation(createDto(route.getDestPoint())).driverId(route.getDriver().getUserId())
				.waypoints(waypoints).everyWorkDay(route.getEveryWorkDay()).build();
	}

	/**
	 * Creates a {@link Route} with the data from <code>routeDto</code>.
	 * 
	 * @param routeDto
	 * @param driver
	 * @return
	 */
	public static Route createRoute(RouteDto routeDto, User driver) {
		Route route = new Route();
		route.setRouteId(routeDto.getRouteId());
		route.setStartAddress(routeDto.getStartAddress());
		route.setDestAddress(routeDto.getDestinationAddress());
		route.setStartTime(routeDto.getStartDateTime());
		route.setArrivalTime(routeDto.getArrivalDateTime());
		route.setStartPoint(createGeoPoint(routeDto.getStarLocation(), route, 0));
		route.setDestPoint(createGeoPoint(routeDto.getDestinationLocation(), route, 1));
		route.setSeats(routeDto.getSeats());
		route.setCar(routeDto.getCar());
		route.setDriver(driver);
		route.setEveryWorkDay(routeDto.getEveryWorkDay());
		return route;
	}

	public static GeoPointDto createDto(GeoPoint geoPoint) {
		return new GeoPointDto(geoPoint.getLat(), geoPoint.getLng());
	}

	public static GeoPoint createGeoPoint(GeoPointDto dto, Route route, int order) {
		GeoPoint geoPoint = new GeoPoint(dto.getLat(), dto.getLng());
		geoPoint.setRoute(route);
		geoPoint.setOrder(order);
		return geoPoint;
	}

	public static RouteRequest createRouteRequest(RouteRequestDto dto, User user, Route route, RouteRequestStatus status) {
		RouteRequest routeReq = new RouteRequest();
		routeReq.setUser(user);
		routeReq.setMessage(dto.getMessage());
		routeReq.setSeats(dto.getSeats());
		routeReq.setRouteRequestStatus(status);
		routeReq.setPickupPoint(dto.getPickupPoint());
		routeReq.setDestinationPoint(dto.getDropOffPoint());
		routeReq.setRoute(route); 
		return routeReq;
	}

	public static RouteRequestDto createDto(RouteRequest routeReq) {
		return new RouteRequestDtoBuilder().id(routeReq.getRrId()).createdBy(routeReq.getUser())
				.message(routeReq.getMessage()).seats(routeReq.getSeats()).creationDate(routeReq.getCreationDate())
				.status(routeReq.getRouteRequestStatus().getStatus()).pickupPoint(routeReq.getPickupPoint())
				.dropOffPoint(routeReq.getDestinationPoint()).routeId(routeReq.getRoute().getRouteId()).build();
	}
}
